<?php
  Template::$folder = 'tmpl/';
  define( '_HOME', getcwd() );
  
  class Config {
    private static $vals = array(), $new_vals = array();
    private static $section = '';
    
    /**
     * Load configuration from database
     * @return void
     */
    public static function load() {
      $sql = "SELECT `key`, `value`, `type`, `section` FROM `config`";
      $res = db::q( $sql );
      while( $r = $res->assoc() ) {
        switch ( $r['type'] ) {
          case 'int':
          case 'integer':
            $val = (int) $r['value'];
            break;
          case 'float':
          case 'double':
            $val = (float) $r['value'];
            break;
          case 'bool':
          case 'boolean':
            $val = (bool) $r['value'];
            break;
          default:
            $val = $r['value'];
        }
        self::$vals[ $r['key'] ] = $val;
      }
    }
    
    
    /**
     * Set section to work with
     * @param string $sec
     * @return var
     */
    public static function section( $sec ) {
      self::$section = $sec;
    }
    
    
    /**
     *  Get value by key
     * @param string $key
     * @return mixed
     */
    public static function get( $key ) {
      $k = self::$section ? self::$section . '.' . $key : $key;
      if ( array_key_exists( $k, self::$vals ) ) {
        return self::$vals[ $k ];
      }
      return null;
    }
    
    
   /**
     * Set value by key
     * @param string $key
     * @param string $val
     * @return void
     */
    public static function set( $key, $val ) {
      $k = self::$section ? self::$section . '.' . $key : $key;
      self::$new_vals[ $k ] = $val;
      self::$vals[ $k ] = $val;
      return self::$new_vals[ $k ];
    }
    
       /**
     * Unset and delete value by key
     * @param string $key
     * @return void
     */
    public static function uset( $key ) {
      $k = self::$section ? self::$section . '.' . $key : $key;
      unset( self::$new_vals[ $k ] );
      unset( self::$vals[ $k ] );
      db::q( "DELETE FROM config WHERE `key`=':1'", $k );
    }
    
    
    
    /**
     * Set or get value by key
     * @param string $key
     * @param string[opt] $val
     * @return mixed
     */
    public static function val( $key, $val=null ) {
      if ( $val === null  ) {
        return self::get( $key );
      }
      return self::set( $key, $val );
    }
    
    
    
    /**
     * Get all values and keys from current section
     * @return array
     */
    public static function all() {
      $res = array();
      if ( self::$section ) {
        $sect = self::$section . '.';
        $l = strlen($sect);
        foreach ( self::$vals as $k => $v ) {
          strpos( $k, $sect )===0 && $res[ substr( $k, $l ) ];
        }
        return $res;
      }
      return self::$vals;
    }
    
    
    /**
     * Save changes to the database
     * @return void
     */
    public static function save() {
      foreach ( self::$new_vals as $key => $val ) {
          $sql = "
             INSERT INTO `config`
             ( `key`, `value`, `type` )
             VALUES
             ( ':1', ':2', ':3' )
             ON DUPLICATE KEY UPDATE
             `value`=':2', `type`=':3'
             ";
          db::q( $sql, $key, $val, gettype($val) );
      }
    }
    
    
    public function __get( $key ) {
      return self::get( $key );
    }
    
    public function __set( $key, $val ) {
      return self::set( $key, $val );
    }
    
    
  }
  
  
  global $conf;
  $conf = new Config();
  Config::load();
  
?>